package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.mapper.user.OrderMapper;
import com.sky.pojo.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.AdminOrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Description TODO
 * @Author bo.li
 * @Date 2024/10/20 17:00
 * @Version 1.0
 */
@Service
public class AdminOrderServiceImpl implements AdminOrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Override
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO dto) {

        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        //test
        Page<OrderVO> page = orderMapper.conditionSearch(dto);

        PageResult pageResult = new PageResult();
        pageResult.setRecords(page.getResult());
        pageResult.setTotal(page.getTotal());

        return Result.success(pageResult);
    }

    @Override
    public OrderStatisticsVO statistics() throws ExecutionException, InterruptedException {
        // 根据状态，   2 用户付款成功，等待商家接单  3.商家接单，等待骑手接单
        //     4 骑手接单成功-待取货  5待送达  6已完成  7已取消
//        Integer waiting4MerchantReceiveOrders = orderMapper.countStatus(Orders.TO_BE_CONFIRMED);
//        Integer waitng4RiderReceiveOrders = orderMapper.countStatus(Orders.CONFIRMED);
//        Integer toBeDeliveryOnProgressOrders = orderMapper.countStatus(Orders.TOBEDELIVERED);
//        Integer toBeArrivedOrders = orderMapper.countStatus(Orders.TOBEPICKEDUP);
//        Integer completedOrders = orderMapper.countStatus(Orders.COMPLETE_ORDER);
//        Integer canceledOrders = orderMapper.countStatus(Orders.CANCELLED_ORDER);

        //利用线程池的方式
//        Future<Integer> waiting4MerchantReceiveOrdersF = threadPoolExecutor.submit(() -> {
//            return orderMapper.countStatus(Orders.TO_BE_CONFIRMED);
//        });
//        Future<Integer> waitng4RiderReceiveOrdersF = threadPoolExecutor.submit(() -> {
//            return orderMapper.countStatus(Orders.CONFIRMED);
//        });
//        Future<Integer> toBeDeliveryOnProgressOrdersF = threadPoolExecutor.submit(() -> {
//            return orderMapper.countStatus(Orders.TOBEDELIVERED);
//        });
//        Future<Integer> toBeArrivedOrdersF = threadPoolExecutor.submit(() -> {
//            return orderMapper.countStatus(Orders.TOBEPICKEDUP);
//        });
//        Future<Integer> completedOrdersF = threadPoolExecutor.submit(() -> {
//            return orderMapper.countStatus(Orders.COMPLETE_ORDER);
//        });
//        Future<Integer> canceledOrdersF = threadPoolExecutor.submit(() -> {
//            return orderMapper.countStatus(Orders.CANCELLED_ORDER);
//        });
//
//        // 将查询出的数据封装到orderStatisticsVO中响应
//        OrderStatisticsVO orderStatisticsVO = OrderStatisticsVO.builder()
//                .waiting4MerchantReceiveOrders(waiting4MerchantReceiveOrdersF.get())//  status = 2  等待商家接单
//                .waitng4RiderReceiveOrders(waitng4RiderReceiveOrdersF.get())//  status = 3  等待骑手接单
//                .toBeDeliveryOnProgressOrders(toBeDeliveryOnProgressOrdersF.get())//  status = 4  待出货订单数
//                .toBeArrivedOrders(toBeArrivedOrdersF.get())//  status = 5  待送达订单数
//                .completedOrders(completedOrdersF.get()) // status = 6  已完成订单数
//                .canceledOrders(canceledOrdersF.get())//  status = 7  已取消订单数
//                .build();
        return orderMapper.orderStatistics();
    }
}